//
//  DoorAddViewController.m
//  RONGAO
//
//  Created by wei wang on 2017/7/6.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "DoorAddViewController.h"
#import <SystemConfiguration/CaptiveNetwork.h>

@interface DoorAddViewController ()
@property (strong, nonatomic) IBOutlet UILabel *wifiName;
@property (strong, nonatomic) IBOutlet UITextField *wifiPassword;
@property (strong, nonatomic) IBOutlet UILabel *wifiLabel;
@property (strong, nonatomic) IBOutlet UIView *normalView;
@property (strong, nonatomic) IBOutlet UIButton *linkBtn;
@property (strong, nonatomic) IBOutlet UIView *failView;

@end

@implementation DoorAddViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self fetchNetInfo];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNavigationStyle];
    [self.linkBtn setBackgroundImage:[UIImage imageNamed:@"login_button_nor"] forState:UIControlStateNormal];
    [self.linkBtn setTitle:@"连接" forState:UIControlStateNormal];
    [self normal];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(fetchNetInfo) name:UIApplicationDidBecomeActiveNotification object:nil];

}

- (void)dealloc
{
    [SVProgressHUD dismiss];

    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [[DTCloudManager defaultJNI_iOS_SDK] stopMatchingNetwork];
}

#pragma mark - the example of how to use executeForResult
- (NSDictionary *)fetchNetInfo
{
    NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
    NSLog(@"%s: Supported interfaces: %@", __func__, interfaceNames);
    
    NSDictionary *SSIDInfo;
    for (NSString *interfaceName in interfaceNames) {
        SSIDInfo = CFBridgingRelease(CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
        
        BOOL isNotEmpty = (SSIDInfo.count > 0);
        if (isNotEmpty) {
            break;
        }
    }
    NSString *ssid = [SSIDInfo objectForKey:@"SSID"] ;
    
    if(ssid)
    {
        NSString *ssids=[NSString stringWithFormat:@"%@",ssid];
        self.wifiName.text = ssids;
        
        NSLog(@"ssidpass==%@",EXRACT_OBJECT(ssid));
        if (EXRACT_OBJECT(ssid)!=nil) {
            
            self.wifiPassword.text=EXRACT_OBJECT(ssid);
            
        }
        self.linkBtn.enabled        = YES;

        self.wifiPassword.enabled = YES;
    }else
    {
        self.wifiName.text           = @"wifi名字";
        self.linkBtn.enabled        = NO;
        self.wifiPassword.enabled = NO;
    }
    return SSIDInfo;
}



- (void)normal{
    
    self.failView.hidden = YES;
    self.normalView.hidden = NO;
    [self.wifiLabel setHighlighted:NO];
    self.wifiLabel.text = @"请确认连接到无线网络并输入密码";
    
}

- (void)fail{
    
    self.failView.hidden = NO;
    self.normalView.hidden = YES;
    [self.wifiLabel setHighlighted:YES];
    self.wifiLabel.text = @"连接失败,请尝试重新连接";
    [self.linkBtn setBackgroundImage:[UIImage imageNamed:@"wifiLink"] forState:UIControlStateNormal];
    [self.linkBtn setTitle:@"重新连接" forState:UIControlStateNormal];
    
}

- (IBAction)perNetwork:(UIButton *)sender {
    
    if (!(self.wifiLabel.text.length != 0 && self.wifiPassword.text.length != 0)) {

        [SVProgressHUD showInfoWithStatus:@"请检查WiFi名字与密码是否输入正确"];
        return;
    }
    [SVProgressHUD showWithStatus:@"配网中"];
    NSLog(@"帐号%@:密码%@",self.wifiName.text,self.wifiPassword.text);
    
    NSString *typeID;
    NSString *deviceName;
    LinkType linkType;
    switch (self.doorType)
    {
        case SmartDoorLock:
        {
            
            typeID = TYPEID3;
            deviceName = @"荣高入户门智能系统";
            linkType = HFLinkMan;
        }
            break;
        case TranslationDoor:
        {
            typeID = TYPEID2;
            deviceName = @"荣高平移门";
            linkType = HFLinkMan;
        }
            break;
        case HouseholdsDoor:
        {
            typeID = TYPEID;
            deviceName = @"荣高智能门锁";
            linkType = LXLinkMan;
        }
            break;
            
        default:
            break;
    }
    
    [[DTCloudManager defaultJNI_iOS_SDK] startDeviceMatchingNetwork:linkType deviceProductId:typeID deviceName:deviceName wifiSSID:self.wifiName.text wifiPassword:self.wifiPassword.text successCallback:^(NSDictionary *dic) {
        
        
        DTDevice *info = dic[@"data"];
        
        if ([info.macAddress length] != 12) {
            
            return [SVProgressHUD showErrorWithStatus:@"绑定失败"];
        }
        
        [[DTCloudManager defaultJNI_iOS_SDK] bindDeviceByName:info.deviceName macAddress:info.macAddress productId:typeID deviceType:DeviceUsedByWiFi successCallback:^(NSDictionary *dic) {
            
            [SVProgressHUD showSuccessWithStatus:@"绑定成功"];
            [self.navigationController popViewControllerAnimated:YES];

        } errorCallback:^(NSDictionary *dic) {
            
            [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];

        }];
        
        
    } errorCallback:^(NSDictionary *dic) {
       
        [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
        [self fail];
    }];
    
    
}




- (void)setNavigationStyle{
    
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];
    [self setNavigationTitle:@"添加设备" titleColor:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
